package com.longcai.piaojia.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.UpdateNamePost;
import com.longcai.piaojia.fragment.MineFragment;
import com.longcai.piaojia.util.MyToast;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;

/**
 * Created by Administrator on 2016/8/31.
 */
public class EditNameActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.edit_name_submit_tv) private TextView edit_name_submit_tv;
    @BoundView(R.id.edit_name_et) private EditText edit_name_et;

    private UpdateNamePost updateNamePost = new UpdateNamePost(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {
            super.onSuccess(toast, type, s);

            MyAccountManagerActivity.getUserInfo getUserInfo = (MyAccountManagerActivity.getUserInfo) getAppCallBack(MyAccountManagerActivity.class);

            getUserInfo.updateName(s);

            MineFragment.getUserAdvert getUserAdvert = (MineFragment.getUserAdvert) getAppCallBack(MineFragment.class);

            getUserAdvert.updateTrueName(s);

            Toast.makeText(EditNameActivity.this, toast, Toast.LENGTH_LONG).show();

            finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(EditNameActivity.this, toast, Toast.LENGTH_LONG).show();

        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name);

        setBackTrue();

        setTitleName(getResources().getString(R.string.edit_name_title_text));

        edit_name_et.setText(getIntent().getStringExtra("truename"));
        edit_name_et.setSelection(getIntent().getStringExtra("truename").length());

        edit_name_submit_tv.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.edit_name_submit_tv:

                String name = edit_name_et.getText().toString().trim();

                if(TextUtils.isEmpty(name)){

                    MyToast.show(context, getString(R.string.edit_name_hint), Toast.LENGTH_LONG);

                    return;

                }

                updateNamePost.name = name;
                updateNamePost.user_id = BaseApplication.BasePreferences.readUserId();
                updateNamePost.type = "1";
                updateNamePost.execute(EditNameActivity.this);

                break;

        }

    }
}
